package com.yanqu.road.dao.impl.activity.shushan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shushan.ShuShanBuildData;
import com.yanqu.road.entity.activity.shushan.ShuShanExerciseSuccessNode;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/3/18 14:58
 */
public class ShuShanBuildDataDaoImpl extends TempDataObjectDao<ShuShanBuildData> {
    public static String getShuShanBuildDataSql = "select * from `t_u_activity_shushan_build_data` where `activityId`=? ";

    public Map<Long, Map<Integer, ShuShanBuildData>> getShuShanBuildDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, ShuShanBuildData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuShanBuildDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanBuildData item = parseData(rs);
                Map<Integer, ShuShanBuildData> userBuildMap = map.get(item.getUserId());
                if(userBuildMap == null){
                    userBuildMap = new ConcurrentHashMap<>();
                    map.put(item.getUserId(), userBuildMap);
                }
                userBuildMap.put(item.getType(), item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateShuShanBuildDataSql = "update `t_u_activity_shushan_build_data` set level=?,freeTime=?,curDiscipleId=?,exerciseStartTime=?,successNodeList=? where activityId=? and userId=? and type=?";

    @Override
    protected boolean updateData(ShuShanBuildData shuShanBuildData) {
        shuShanBuildData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getFreeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getCurDiscipleId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getExerciseStartTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanBuildData.getSuccessNodeList())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanBuildData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getType()));
        if (getDb().executeNoneQuery(updateShuShanBuildDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuShanBuildDataSql = "insert into `t_u_activity_shushan_build_data`(activityId,userId,type,level,freeTime,curDiscipleId,exerciseStartTime,successNodeList) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ShuShanBuildData shuShanBuildData) {
        shuShanBuildData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanBuildData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getFreeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getCurDiscipleId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanBuildData.getExerciseStartTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanBuildData.getSuccessNodeList())));
        if (getDb().executeNoneQuery(insertShuShanBuildDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuShanBuildData parseData(ResultSet rs) throws SQLException {
        ShuShanBuildData shuShanBuildData = new ShuShanBuildData();
        shuShanBuildData.setActivityId(rs.getInt("activityId"));
        shuShanBuildData.setUserId(rs.getLong("userId"));
        shuShanBuildData.setType(rs.getInt("type"));
        shuShanBuildData.setLevel(rs.getInt("level"));
        shuShanBuildData.setFreeTime(rs.getInt("freeTime"));
        shuShanBuildData.setCurDiscipleId(rs.getInt("curDiscipleId"));
        shuShanBuildData.setExerciseStartTime(rs.getInt("exerciseStartTime"));
        shuShanBuildData.setSuccessNodeList(JSON.parseObject(rs.getString("successNodeList"), new TypeReference<List<ShuShanExerciseSuccessNode>>() {
        }));

        return shuShanBuildData;
    }

}
